package com.saltedfish.tool.dialog;

import android.app.Activity;
import android.os.Build;
import android.text.TextUtils;
import android.view.Gravity;
import android.view.View;
import android.webkit.WebChromeClient;
import android.webkit.WebSettings;
import android.webkit.WebView;
import android.webkit.WebViewClient;
import android.widget.ImageView;
import android.widget.TextView;

import com.saltedfish.tool.R;

import butterknife.BindView;
import cn.droidlover.xdroidmvp.popwindow.DialogHelper;

public class UIServiceDialog extends DialogHelper {

    @BindView(R.id.tvBack)
    ImageView tvBack;

    @BindView(R.id.tvTitle)
    TextView tvTitle;

    @BindView(R.id.web_view)
    WebView webView;

    public UIServiceDialog(Activity context) {
        super(context, R.style.AlertDialogStyle);
        this.mContext = context;
        setFullWidthScreen();
        setGravity(Gravity.BOTTOM);
        setAnimSet(R.style.popBottomPopAnim);
        setSettings();

    }

    @Override
    protected int getLayoutId() {
        return R.layout.dialog_ui_service;
    }

    public void initService(String type) {
        String url = "";
        if(type.equals("userService")){
            url = "https://hengfengstudio.com/ow/privacy/user.html";
            tvTitle.setText("用户服务协议");
        }else {
            url = "https://hengfengstudio.com/ow/privacy/yinsi.html";
            tvTitle.setText("隐私保护政策");
        }
        webView.loadUrl(url);

    }
        @Override
    protected void initView() {
        tvBack.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View view) {
                dismiss();
            }
        });
    }


    private void setSettings(){
        webView.setWebViewClient(new WebViewClient());
        webView.setWebChromeClient(new WebChromeClient(){
            @Override
            public void onReceivedTitle(WebView view, String title) {
                super.onReceivedTitle(view, title);
//                if(!TextUtils.isEmpty(title) &&  !title.contains("http")){
//                    tvWebTitle.setText(title);
//                }
            }

            @Override
            public void onProgressChanged(WebView view, int newProgress) {
                super.onProgressChanged(view, newProgress);
//                if(pbProgress != null){
//                    pbProgress.setProgress(newProgress);
//                    if(newProgress >= 95){
//                        pbProgress.setVisibility(View.GONE);
//                    }
//                }
            }
        });
        WebSettings settings = webView.getSettings();
//        settings.setAppCacheEnabled(true);
//        settings.setAppCachePath(context.getCacheDir().getAbsolutePath());
        settings.setLayoutAlgorithm(WebSettings.LayoutAlgorithm.SINGLE_COLUMN);
        settings.setDomStorageEnabled(true);
        settings.setDatabaseEnabled(true);
        settings.setUseWideViewPort(true);
        settings.setLoadWithOverviewMode(true);
        settings.setJavaScriptEnabled(true);
        settings.setTextZoom(170);
     //   settings.setTextSize(WebSettings.TextSize.LARGER);

        if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.LOLLIPOP) {
            // 5.0以上允许加载http和https混合的页面(5.0以下默认允许，5.0+默认禁止)
            settings.setMixedContentMode(WebSettings.MIXED_CONTENT_ALWAYS_ALLOW);
        }
        settings.setCacheMode(WebSettings.LOAD_DEFAULT);
//        webView.addJavascriptInterface(new CallBackJs(),"android");
    }
    @Override
    protected void initData() {

    }

    @Override
    protected void dialogDismiss() {

    }


}
